package com.errorfinding.tabbitmqDemo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于插件的配置类
 */
@Configuration
public class DelayedQueueConfig {

    //交换机
    public static final String delayed_exchange_name = "delayed.exchange";
    //队列
    public static final String delayed_queue_name = "delayed.queue";
    //routingkey
    public static final String delayed_routing_key = "delayed.routingkey";

    //声明队列
    @Bean
    public Queue delayedQueue(){
        return new Queue(delayed_queue_name);
    }

    //声明交换机 基于插件
    @Bean
    public CustomExchange delayedExchange(){
        /**
         * CustomExchange 自定义交换机类型
         * 参数1.交换机名称
         * 参数2.交换机类型
         * 参数3.是否需要持久化
         * 参数4.是否需要自动删除
         * 参数5.其他参数
         */
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(delayed_exchange_name,"x-delayed-message",true,false,arguments);
    }

    //绑定
    @Bean
    public Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue") Queue delayedQueue,
                                                      @Qualifier("delayedExchange") CustomExchange delayedExchange){

        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(delayed_routing_key).noargs();
    }

}
